//
//  UserAgreementView.m
//  TengyunStation
//
//  Created by Johnny on 15/11/24.
//  Copyright © 2015年 xingrong. All rights reserved.
//

#import "UserAgreementView.h"
#import "TSHeader.h"
#import "Singleton.h"

#define AlertWidth          272.0
#define SpaceTop            65.0

@implementation UserAgreementView

- (id)initWithDelegate:(id<UserAgreementViewDelegate>)delegate {
    CGSize size = [UIScreen mainScreen].bounds.size;
    float x = (size.width - AlertWidth)/2.0;
    float height = size.height - SpaceTop * 2;
    self = [super initWithFrame:CGRectMake(x, SpaceTop, AlertWidth, height)];
    if (self) {
        self.backgroundColor = [UIColor whiteColor];
        self.layer.cornerRadius = 4;
        self.clipsToBounds = YES;
        _delegate = delegate;
        UILabel *titleLabel = [[UILabel alloc] initWithFrame:CGRectMake(5, 10, AlertWidth - 10, 20)];
        titleLabel.font = [UIFont systemFontOfSize:15];
        titleLabel.textAlignment = NSTextAlignmentCenter;
        titleLabel.textColor = MainTextColor;
        titleLabel.text = @"腾云小站快件收入政策";
        [self addSubview:titleLabel];
        
        UILabel *topLine = [[UILabel alloc] initWithFrame:CGRectMake(0, 40, AlertWidth, 0.5)];
        topLine.backgroundColor = LineColor;
        [self addSubview:topLine];
        
//        [[NSURLCache sharedURLCache] removeAllCachedResponses];
        UIWebView *webView = [[UIWebView alloc] initWithFrame:CGRectMake(5, 45, AlertWidth - 10, height - 40 - 48 - 10)];
        webView.backgroundColor = [UIColor whiteColor];
        NSURLRequest *request = [NSURLRequest requestWithURL:[NSURL URLWithString:[NSString stringWithFormat:@"%@/documents/tyxz_cooperative_side_agreement.html",[Singleton shareInstance].cityModel.manageSystemUrl]]];
//        NSURLRequest *request = [NSURLRequest requestWithURL:[NSURL URLWithString:@"http://192.168.128.37:18081/TYXZ_OMMS/documents/tyxz_cooperative_side_agreement.html"]];
        [webView loadRequest:request];
        webView.delegate = self;
        [self addSubview:webView];
        
        _activityIndicatorView = [[UIActivityIndicatorView alloc] initWithActivityIndicatorStyle:UIActivityIndicatorViewStyleGray];
        _activityIndicatorView.center = webView.center;
        [_activityIndicatorView startAnimating];
        [self addSubview:_activityIndicatorView];
        
        UILabel *bottomLine = [[UILabel alloc] initWithFrame:CGRectMake(0, height - 48.0, AlertWidth, 0.5)];
        bottomLine.backgroundColor = LineColor;
        [self addSubview:bottomLine];
        
        _selectedBtn = [UIButton buttonWithType:UIButtonTypeCustom];
        _selectedBtn.frame = CGRectMake(10, bottomLine.frame.origin.y + 9, 30, 30);
        [_selectedBtn setImage:[UIImage imageNamed:@"btn_yongh_no"] forState:UIControlStateNormal];
        [_selectedBtn setImage:[UIImage imageNamed:@"btn_yongh_yes"] forState:UIControlStateSelected];
        [_selectedBtn addTarget:self action:@selector(selectedBtnAction:) forControlEvents:UIControlEventTouchUpInside];
        [self addSubview:_selectedBtn];
        
        UILabel *label = [[UILabel alloc] initWithFrame:CGRectMake(45, bottomLine.frame.origin.y + 14, 100, 20)];
        label.font = [UIFont systemFontOfSize:14];
        label.textColor = MainTextColor;
        label.text = @"我已认真阅读";
        [self addSubview:label];
        
        _agressBtn = [UIButton buttonWithType:UIButtonTypeCustom];
        _agressBtn.frame = CGRectMake(AlertWidth - 122 - 10, bottomLine.frame.origin.y + 9, 122, 30);
        [_agressBtn setTitle:@"同意" forState:UIControlStateNormal];
        _agressBtn.titleLabel.font = [UIFont systemFontOfSize:15];
        [_agressBtn setTitleColor:[UIColor whiteColor] forState:UIControlStateNormal];
        _agressBtn.backgroundColor = UIColorFromRGB(0xc1c1c1);
        [_agressBtn addTarget:self action:@selector(agreeBtnAction) forControlEvents:UIControlEventTouchUpInside];
        _agressBtn.layer.cornerRadius = 4;
        _agressBtn.clipsToBounds = YES;
        [self addSubview:_agressBtn];
        
        [self selectedBtnAction:_selectedBtn];
    }
    return self;
}

- (void)selectedBtnAction:(UIButton *)button {
    if (button.selected) {
        button.selected = NO;
        _agressBtn.backgroundColor = UIColorFromRGB(0xc1c1c1);
    } else {
        button.selected = YES;
        _agressBtn.backgroundColor = OrangeColor;
    }
}

- (void)agreeBtnAction {
    if (_selectedBtn.selected) {
        if ([_delegate respondsToSelector:@selector(userAgreementViewAgreeAction)]) {
            [_delegate userAgreementViewAgreeAction];
            [self removeAlertWindow];
        }
    }
}

- (void)show {
    if (!_alertWindow) {
        _alertWindow = [[UIWindow alloc] initWithFrame:[UIScreen mainScreen].bounds];
        _alertWindow.backgroundColor = [UIColor colorWithRed:0 green:0 blue:0 alpha:0.5];
        _alertWindow.windowLevel = UIWindowLevelNormal;
    }
    [_alertWindow addSubview:self];
    [_alertWindow makeKeyAndVisible];
}

- (void)removeAlertWindow {
    [_alertWindow resignFirstResponder];
    [_alertWindow resignKeyWindow];
    _alertWindow.alpha = 0;
    _alertWindow = nil;
}

#pragma mark UIWebViewDelegate
- (void)webViewDidFinishLoad:(UIWebView *)webView {
    [_activityIndicatorView stopAnimating];
    _activityIndicatorView.hidden = YES;
}

- (void)webView:(UIWebView *)webView didFailLoadWithError:(NSError *)error {
    [_activityIndicatorView stopAnimating];
    _activityIndicatorView.hidden = YES;
}

@end
